package com.group5.onlinegame.handler;

import com.github.pagehelper.PageInfo;
import com.group5.onlinegame.entity.*;
import com.group5.onlinegame.service.ICommonService;
import com.group5.onlinegame.service.IUserService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.*;
import org.springframework.web.multipart.MultipartFile;
import org.springframework.web.servlet.ModelAndView;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import javax.servlet.http.HttpSession;
import java.io.File;
import java.io.IOException;
import java.sql.Timestamp;
import java.util.*;

@Controller
@RequestMapping("/")
public class UserMsgHandler {
    @Autowired
    private IUserService userService;
    @Autowired
    private ICommonService iCommonService;
    //查询并显示用户
    @RequestMapping("usermsg")
    public ModelAndView getRRList( @RequestParam(name = "pageNum",defaultValue = "1",required = false)Integer cpage,
                                   @RequestParam(name="input",defaultValue = "0",required = false) String input,
                                   @RequestParam(name="city",defaultValue = "0",required = false) int city,
                                   @RequestParam(name = "cityi",defaultValue = "0",required = false)String cityi ){
        ModelAndView mv=new ModelAndView("usermsg");
        User user=new User();
        Vip vip=new Vip();
        Roles roles=new Roles();
        if (city==1){
            user.setUsername(input);
        }else if (city==2){
            vip.setId(Integer.parseInt(cityi));
        }else if (city==3){
            roles.setId(Integer.parseInt(cityi));
        }
        user.setVip(vip);
        user.setRoles(roles);
        PageInfo<User> pageInfo=userService.getUserList(cpage,user);
        mv.addObject("input",input);
        mv.addObject("city",city);
        mv.addObject("cityi",cityi);
        mv.addObject("pageInfo",pageInfo);
        return mv;
    }

    //查询所有vip
    @RequestMapping("showvip")
    @ResponseBody
    public List<Vip> showvip(){
        List<Vip> list=userService.getVipList();
        return list;
    }
    //全选删除
    @RequestMapping("delall")
    @ResponseBody
    public String delall(@RequestParam(value = "listid[]") int[] id){
        try {
            userService.delall(id);
        }catch (Exception e){
            e.printStackTrace();
            return "1";
        }
        return "0";
    }
    //删除一个
    @RequestMapping("delone")
    @ResponseBody
    public String delone(@RequestParam(value = "id") int id){
        try {
            userService.delone(id);
        }catch (Exception e){
            e.printStackTrace();
            return "1";
        }
        return "0";
    }
    //上传图片
    @RequestMapping("upphoto")
    public String upphoto(MultipartFile photo,HttpSession session){
        String path=session.getServletContext().getRealPath("upload/");
        File file=new File(path+photo.getOriginalFilename());
        try {
            photo.transferTo(file);
        } catch (IOException e) {
            e.printStackTrace();
            return "redirect:/usermsg";
        }
        return "redirect:/usermsg";
    }

    //查询数据库是否有重复的用户名
    @RequestMapping("checkusername")
    @ResponseBody
    public String checkusername(@RequestParam(value = "username") String username){
        System.out.println(username);
        int i=userService.countUserByName(username);
        String c=String.valueOf(i);
        return c;
    }

    //添加用户
    @RequestMapping(value = "adduser",method = RequestMethod.POST)
    @ResponseBody
    public String adduser(@RequestBody User user,@RequestParam(value = "v")String v,@RequestParam(value = "r")String r){
        String fName=user.getPhoto();
        File tempFile =new File( fName.trim());
        String fileName = tempFile.getName();
        user.setPhoto(fileName);
        int id=Integer.parseInt(v);
        Vip vip=new Vip(id,null,null,null,null);
        user.setVip(vip);
        int s=userService.getSeq();
        user.setId(s);
        UserRole userRole=new UserRole();
        userRole.setUserid(s);
        userRole.setRoleid(r);
        String uuid = UUID.randomUUID().toString().replaceAll("-", "");
        System.out.println(uuid);
        user.setNickname("游客"+uuid);
        System.out.println(user);
       try{
           userService.adduser(user);
           userService.adduserrole(userRole);
       }catch (Exception e){
           e.printStackTrace();
           return "1";
       }
       return "0";
    }
    //显示编辑弹出层
    //查询所有user
    @RequestMapping("showuser")
    @ResponseBody
    public User showvip(@RequestParam(value = "id") int id){
        User user=userService.countUserById(id);
        return user;
    }
    //doedit
    @RequestMapping("doedit")
    @ResponseBody
    public String doedit(@RequestBody User user,@RequestParam(value = "v")String v){
        String fName=user.getPhoto();
        File tempFile =new File( fName.trim());
        String fileName = tempFile.getName();
        user.setPhoto(fileName);
        int id=Integer.parseInt(v);
        Vip vip=new Vip(id,null,null,null,null);
        user.setVip(vip);
        System.out.println(user);
        try{
            userService.editByid(user);
        }catch (Exception e){
            e.printStackTrace();
            return "1";
        }
        return "0";
    }
    //查询所有角色
    @RequestMapping("showallroles")
    @ResponseBody
    public List<Roles> showallroles(){
        List<Roles> list=userService.getUserListRoles();
        return list;
    }
    //添加用户
    @RequestMapping(value = "edituser",method = RequestMethod.POST)
    @ResponseBody
    public String edituser(@RequestBody User user,@RequestParam(value = "v")String v,@RequestParam(value = "r")String r){
        String fName=user.getPhoto();
        File tempFile =new File( fName.trim());
        String fileName = tempFile.getName();
        user.setPhoto(fileName);
        int id=Integer.parseInt(v);
        Vip vip=new Vip(id,null,null,null,null);
        user.setVip(vip);
        UserRole userRole=new UserRole();
        userRole.setUserid(user.getId());
        userRole.setRoleid(r);
        System.out.println(user);
        try{
            userService.editByid(user);
            userService.editUserRoleById(userRole);
        }catch (Exception e){
            e.printStackTrace();
            return "1";
        }
        return "0";
    }
    /*先获值在传页面*/
    @RequestMapping("getnumbytime")
    @ResponseBody
    public ModelAndView getnumbytime(){
        ModelAndView mv=new ModelAndView("statistics2");

        List<String> strings=new ArrayList<>();
        List<Integer> integers=new ArrayList<>();
        /*时间集合*/
        List<Timestamp> time=userService.gettime();

        Calendar calendar = Calendar.getInstance();
        Map<Integer,Integer> map=new TreeMap<>();
        for (Timestamp t:time){
            calendar.setTime(t);
            int id=calendar.get(Calendar.MONTH);
            if (map.containsKey(id)){
                Integer val=map.get(id);
                val++;
                map.put(id,val);
            }
            else{
                map.put(id,1);
            }
        }

        for(Integer key:map.keySet()){
            strings.add(String.valueOf(key)+"月份");
        }
        Collection<Integer> collection=map.values();
        for (Integer i:collection){
            integers.add(i);
        }
        Demo demo=iCommonService.getget2(strings,integers);
        mv.addObject("data",demo);
        return mv;
    }
    //启用或禁用
    @RequestMapping("onoroff")
    @ResponseBody
    public String onoroff(String status,String id){
        User user=new User();
        int s=Integer.parseInt(status);
        user.setStatus(s);
        int i=Integer.parseInt(id);
        user.setId(i);
        try{
        userService.onoroff(user);
        }catch (Exception e){
            return "1";
        }
        return "0";
    }
}
